<template>
	<view class="container">
		<!-- 导航栏 -->
		<u-navbar title="等级升级" :title-bold="true" title-color="#000" :border-bottom="false" custom-class="home-bg" :background="{ background: 'transparent' }"></u-navbar>

		<!-- 主体内容 -->
		<view class="content">
			<view class="card-style">
				<!-- 当前等级卡片 -->
				<view class="list-item">
					<text class="item-label">当前等级</text>
					<text class="item-value">{{ user.memberName || 'memberName' }}</text>
				</view>

				<!-- 申请等级卡片 -->
				<view class="list-item">
					<text class="item-label">申请等级</text>
					<text class="item-value" @click="show = true">{{ grade ? grade : '请选择等级' }}</text>
				</view>
				<u-select v-model="show" value-name="id" label-name="memberName" :list="list" @confirm="confirm"></u-select>

				<!-- 上传凭证卡片 -->
				<view class="upload-area">
					<text class="upload-text">上传付款凭证</text>

					<u-upload ref="uUpload" @on-uploaded="handleAllUploaded" :header="header" :auto-upload="false" :action="action"></u-upload>
				</view>

				<!-- 个人信息卡片 -->
				<!-- :disabled="true" -->
				<u-field label="姓名" :placeholder="name" inputAlign="right"></u-field>
				<u-field label="电话号码" :placeholder="phonenumber" inputAlign="right"></u-field>

				<!-- 电子合同卡片 -->
				<!-- 		<view class="contract-item">
					<view style="display: flex; flex-direction: column">
						<text class="item-label">电子合同</text>
						<text class="contract-title">《等级升级电子合同》</text>
					</view>
					<u-button size="mini" shape="circle" type="error">去签订</u-button>
				</view> -->
			</view>

			<!-- 底部提示 -->

			<!-- 	<text class="bottom-tip">
				<u-icon name="error-circle" size="35" style="margin-right: 7rpx"></u-icon>
				<text>请先购买甄选礼包，再进行等级升级</text>
			</text> -->
		</view>

		<view class="flex align-center buttom">
			<button @click="setVip()" :disabled="userVipIndex == 1 ? true : false">{{ userVipIndex == 1 ? '待审核' : '提交' }}</button>
		</view>
	</view>
</template>

<script>
import user from '@/api/user';
import api from '@/api/index.js';
export default {
	data() {
		return {
			name: '',
			phonenumber: '',
			action: `${api.getPathApi()}/common/upload`,
			show: false,
			list: [],
			grade: '',
			files: [], //上传图片列表
			header: {},
			applyMemberId: 0,
			user: {},
			userVipIndex: 0
		};
	},
	onLoad(o) {
		this.getVip();
		let token = uni.getStorageSync('token');
		this.header.Authorization = 'Bearer ' + token;
		this.user = uni.getStorageSync('user');
		this.name = this.user.nickName;
		this.phonenumber = this.user.phonenumber;
		this.getVipSelect();
	},
	methods: {
		confirm(e) {
			this.grade = e[0].label;
			this.applyMemberId = e[0].value;
		},
		getVip() {
			user.getVip().then((res) => {
				this.list = res.rows;
			});
		},
		setVip() {
			this.$refs.uUpload.upload();
		},
		async handleAllUploaded(lists, name) {
			// 假设 lists 是文件列表数组，每个元素包含 response 属性
			this.files = lists
				.map((item) => {
					// 确保 response 存在且 code 为 200
					if (item.response?.code === 200) {
						return item.response.fileName; // 提取文件名
					}
					return null; // 不符合条件的返回 null
				})
				.filter(Boolean); // 过滤掉 null 或 undefined

			const res = await user.setVip({
				applyMemberId: this.applyMemberId,
				applyCertificate: this.files.join(',')
			});
			if (res.code == 200) {
				uni.showToast({
					title: '提交成功，请等待审核',
					icon: 'success'
				});
				uni.navigateBack();
			} else {
				uni.showToast({
					title: '提交失败',
					icon: 'error'
				});
			}
		},
		getVipSelect() {
			user.getVipSelect().then((res) => {
				this.userVipIndex = res.rows[0].auditStatus;
				if (this.userVipIndex == 3) {
					uni.showToast({
						title: '审核驳回',
						icon: 'none'
					});
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.flex {
	display: flex;
}
.align-center {
	align-items: center;
}
.flex-center {
	justify-content: center;
}
.flex-between {
	justify-content: space-between;
}
.flex-column {
	flex-direction: column;
}
.container {
	background-color: #f5f5f5;
	min-height: 100vh;
}
.buttom {
	position: fixed;
	bottom: 0;
	left: 50%;
	transform: translate(-50%);
	height: 120rpx;
	width: 100%;
	background-color: #fff;
	border-radius: 30rpx 30rpx 0 0;
	display: flex;
	justify-content: center;
	align-items: center;
	button {
		border-radius: 100rpx;
		width: 500rpx;
		background-color: #f05a39;
		color: #fff;
	}
}

.content {
	padding: 12px;
}

.card-style {
	background: #fff;
	border-radius: 8px;
	margin-bottom: 12px;
	padding: 0 16px;
	box-shadow: 0 2px 6px rgba(0, 0, 0, 0.03);
}

.list-item {
	display: flex;
	justify-content: space-between;
	padding: 16px 0;
	border-bottom: 1rpx solid #999;
	.item-label {
		color: #333;
		font-size: 15px;
	}
	.item-value {
		color: #666;
		font-size: 14px;
	}
}

.upload-area {
	padding: 20px 0;
	margin: 0;
	.upload-text {
		color: #333;
		font-size: 15px;
		display: block;
		margin: 15rpx 0;
	}
}

.contract-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 16px 0;
	.contract-title {
		display: block;
		color: #000;
		font-size: 14px;
		padding: 10rpx 20rpx;
		background-color: #f5f5f5;
		border-radius: 10rpx;
	}
}

.bottom-tip {
	color: #e4393c;
	font-size: 12px;
	margin: 20px 0;
}
</style>
